package com.congee02.daemon;

import com.congee02.ThreadProvider;

/**
 * @author congee02
 * @date 2/1/2023 2:52 PM
 */
public class DaemonThreadTest {

    public static void main(String[] args) {
        ThreadProvider provider = new CustomDaemonThreadProvider();
        Thread daemonThread = provider.thread();
        
        // 将线程设置为守护进程，当其他进程(这里只指 main 线程)结束时，守护进程自动结束
        daemonThread.setDaemon(true);
        daemonThread.start();
        
        for (int i = 0 ; i < 10 ; i ++ ) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
        }
    }
    
    private static class CustomDaemonThreadProvider implements ThreadProvider {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "RUNNING ...");
            }
        }
    }
    
}
